// Author: wsfuyibing <682805@qq.com>
// Date: 2025-06-18

package return_fish

import (
	"server/app/errs"
	"server/app/kind"
)

// Request
// 入参.
type Request struct {
	Id           int             `validate:"required,min=1" label:"购票记录ID"`
	ReturnMode   kind.ReturnMode `validate:"required,min=1" label:"回鱼模式"`
	ReturnPrice  kind.Balance    `label:"回鱼价格"`
	ReturnAmount kind.Balance    `label:"回鱼金额"`
	ReturnCount  float64         `label:"回鱼条数"`
	ReturnWeight float64         `label:"回鱼重量"`
}

// AfterValidate
// 后置校验.
func (o *Request) AfterValidate() (err error) {
	// 1. 不回鱼.
	//    必要的字段置为零值.
	if o.ReturnMode.IsNot() {
		o.ReturnAmount = 0
		o.ReturnPrice = 0
		o.ReturnCount = 0
		o.ReturnWeight = 0
		return
	}

	// 2. 金额错误.
	//    最低回鱼金额不能少于0.5元.
	if o.ReturnAmount < 0.5 {
		err = errs.ErrTicketRecordReturnAmountInvalid
		return
	}

	// 3. 重量错误.
	//    最低回鱼重量不能少于0.5斤.
	if o.ReturnMode.IsWeight() && o.ReturnWeight < 0.5 {
		err = errs.ErrTicketRecordReturnWeightInvalid
		return
	}

	// 4. 数量错误.
	//    最低回鱼数量不能少于1条.
	if o.ReturnMode.IsCount() && o.ReturnCount < 1 {
		err = errs.ErrTicketRecordReturnCountInvalid
		return
	}

	// n. 校验成功.
	return
}
